from mailstore import MailStore, MailFolder
from imap import Imap4MailStore, Imap4SSLMailStore

__all__ = ['MailFolder', 'MailStore', 'MailStoreFactory']

def MailStoreFactory(uri):
    """
    Call this method with the uri of a server to obtain an
    initialised MailStore that knows how to deal with that
    protocol.
    
    If an unknown uri scheme is passed it will raise ValueError.

    Currently supports:
        imap:// for IMAP, with STARTTLS if offered
        imaps:// for IMAP over SSL.
    """

    host = None
    port = None

    ### FIXME: This doesn't handle user parts or much actually.
    ### Perhaps urlparse could help?
    proto, rest = uri.split('://',1)
    if rest.find('/') != -1:
        host, rest = rest.split('/',1)
    else:
        host = rest

    if host.find(':') != -1:
        host, port = host.split(':',1)

    if proto == 'imaps':
        return Imap4SSLMailStore(host, port)
    if proto == 'imap':
        return Imap4MailStore(host, port)
    raise ValueError
